#pragma once
#include<iostream>
#include"Connection.hpp"
#include"Socket.hpp"
#include"NetCal.hpp"
class PackageParse
{
public:
    void Execute(Connection* con)
	{
		while(true)
		{
			std::string package = Decode(con->InBuffer());
			if(package.size()==0)
				break;//没有完整的有效载荷
			std::cout << "package: \n" << package << std::endl;
			//3.反序列化
			shared_ptr<Request> req = make_shared<Request>();
			req->Deserialize(package);
			//4.业务处理
			shared_ptr<Response> res=NetCal::Calculator(req);
			//5.序列化
			std::string jsonres;
			res->Serialize(&jsonres);
			std::cout << "jsonres: \n" << jsonres << std::endl;
			//6.添加报头
			jsonres=Encode(jsonres);
			std::cout << "jsonres add header done: \n" << jsonres << std::endl;
			//7.发送出去
			con->AddOutBuffer(jsonres);
		};
		if(!con->OutBuffer().empty())
			con->_R->EnableConnectionReadWrite(con->Sockfd(),true,true);
	}
};